package com.quanyan.stadium.biz.impl;

import com.quanyan.common.Plugin.Page;
import com.quanyan.stadium.biz.BizDatesportJoinInfoDao;
import com.quanyan.stadium.entity.TbDatesportJoinInfo;
import com.quanyan.stadium.entity.TbDatesportJoinInfoExample;
import com.quanyan.stadium.entity.vo.GenderStructure;
import com.quanyan.stadium.mapper.DatesportMapper;
import com.quanyan.stadium.mapper.TbDatesportJoinInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zheng.Ke
 * Date 2016/7/14.
 */
@Service
public class BizDatesportJoinInfoDaoImpl implements BizDatesportJoinInfoDao {
    @Autowired
    private TbDatesportJoinInfoMapper tbDatesportJoinInfoMapper;
    @Autowired
    private DatesportMapper datesportMapper;

    @Override
    public TbDatesportJoinInfo getByDatesportIdAndUid(int datesportId, int uid) {
        TbDatesportJoinInfoExample example = new TbDatesportJoinInfoExample();
        TbDatesportJoinInfoExample.Criteria criteria = example.createCriteria();
        criteria.andDatesportIdEqualTo(datesportId);
        criteria.andUidEqualTo(uid);

        List<TbDatesportJoinInfo> list = tbDatesportJoinInfoMapper.selectByExample(example);
        if (list.size() <= 0) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<TbDatesportJoinInfo> findByDatesportIdAndUid(List<Integer> datesportIdList, int uid) {
        TbDatesportJoinInfoExample example = new TbDatesportJoinInfoExample();
        TbDatesportJoinInfoExample.Criteria criteria = example.createCriteria();
        criteria.andDatesportIdIn(datesportIdList);
        criteria.andUidEqualTo(uid);

        return tbDatesportJoinInfoMapper.selectByExample(example);
    }

    @Override
    public List<TbDatesportJoinInfo> findByDatesportIdAndUid(int datesportId, List<Integer> uidList) {
        if (uidList == null || uidList.size() <= 0 ) {
            return new ArrayList<TbDatesportJoinInfo>();
        }
        TbDatesportJoinInfoExample example = new TbDatesportJoinInfoExample();
        TbDatesportJoinInfoExample.Criteria criteria = example.createCriteria();
        criteria.andDatesportIdEqualTo(datesportId);
        criteria.andUidIn(uidList);

        return tbDatesportJoinInfoMapper.selectByExample(example);
    }

    @Override
    public List<TbDatesportJoinInfo> findByDatesportIdAndStatus(int datesportId, List<Byte> status) {
        return findByDatesportIdAndStatus(datesportId, status, null, null);
    }

    @Override
    public List<TbDatesportJoinInfo> findByDatesportIdAndStatus(int datesportId, List<Byte> status, Integer pageNum, Integer pageSize) {
        TbDatesportJoinInfoExample example = new TbDatesportJoinInfoExample();
        TbDatesportJoinInfoExample.Criteria criteria = example.createCriteria();

        criteria.andDatesportIdEqualTo(datesportId);
        criteria.andStatusIn(status);
        if (pageNum != null && pageSize != null) {
            example.setPage(new Page(pageNum*pageSize, pageSize));
        }
        return tbDatesportJoinInfoMapper.selectByExample(example);
    }

    @Override
    public List<TbDatesportJoinInfo> findJoinedByDatesportId(int datesportId) {
        List<Byte> status = new ArrayList<Byte>();
        status.add(new Byte("3"));//申请加入通过
        status.add(new Byte("7"));//已同意邀请加入
        status.add(new Byte("6"));//创建者
        return this.findByDatesportIdAndStatus(datesportId, status);
    }

    @Override
    public List<TbDatesportJoinInfo> findJoinedByDatesportId(int datesportId, Integer pageNum, Integer pageSize) {
        List<Byte> status = new ArrayList<Byte>();
        status.add(new Byte("3"));//申请加入通过
        status.add(new Byte("7"));//已同意邀请加入
        status.add(new Byte("6"));//创建者
        return this.findByDatesportIdAndStatus(datesportId, status, pageNum, pageSize);
    }

    @Override
    public void saveOrUpdateDatesportJoinInfo(TbDatesportJoinInfo tbDatesportJoinInfo) {
        TbDatesportJoinInfo flag = this.getByDatesportIdAndUid(tbDatesportJoinInfo.getDatesportId(), tbDatesportJoinInfo.getUid());
        if (flag == null) {
            tbDatesportJoinInfoMapper.insertSelective(tbDatesportJoinInfo);
            return;
        }
        tbDatesportJoinInfo.setId(flag.getId());
        tbDatesportJoinInfo.setCreateTime(flag.getCreateTime());
        tbDatesportJoinInfoMapper.updateByPrimaryKeySelective(tbDatesportJoinInfo);
    }

    @Override
    public Byte getMaxJoinedPeopleGender(int datesportId, Byte newGender, boolean isAdd) {
        List<GenderStructure> genderStructureList = datesportMapper.getJoinedPeopleGenderStructure(datesportId);

        int increment = 1;
        if (!isAdd) {
            increment = -1;
        }
        int manCnt = 0;
        int womenCnt = 0;
//        int unkownCnt = 0;
        for (GenderStructure gs : genderStructureList) {
            Byte gender = gs.getGender();
            if (gender == null) {
                continue;
            }
            if (gender == 1) { //男
                manCnt = gs.getCount();
            } else if (gender == 2) { // 女
                womenCnt = gs.getCount();
            } else if (gender == 0) { //未知
//                unkownCnt = gs.getCount();
            }
        }
        Byte rtGender = null;
        if (newGender != null) {
            if (newGender == 1) {//男
                manCnt += increment;
            } else if (newGender == 2){//女
                womenCnt += increment;
            }
        }
        if (manCnt < womenCnt) {
            rtGender = 2;
        } else if (manCnt > womenCnt) {
            rtGender = 1;
        } else if (manCnt == womenCnt) {
            rtGender = 0;
        }
        return rtGender;
    }

}
